package com.wxg.address;

import java.io.IOException;

import com.wxg.address.model.Person;
import com.wxg.address.view.PersonEditDialogController;
import com.wxg.address.view.PersonOverviewController;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 * date : 2018年2月12日<br>
 * [JavaFX 8 教程 （中文）](http://code.makery.ch/library/javafx-8-tutorial/zh-cn/)<br>
 * 1) 我们没有设置`birthday`到标签中, 因为它是 `localDate`类型,不是String,我们需要格式化日期。<br>
 *
 *
 * @author Wxg
 *
 */
public class MainApp extends Application {

	private Stage primaryStage;
	private BorderPane rootLayout;

	private ObservableList<Person> personData = FXCollections.observableArrayList();

	/**
	 * Constructor
	 */
	public MainApp(){
		// Add some sample data
		personData.add(new Person("Hans", "Muster"));

		personData.add(new Person("Ruth", "Mueller"));
        personData.add(new Person("Heinz", "Kurz"));
        personData.add(new Person("Cornelia", "Meier"));
        personData.add(new Person("Werner", "Meyer"));
        personData.add(new Person("Lydia", "Kunz"));
        personData.add(new Person("Anna", "Best"));
        personData.add(new Person("Stefan", "Meier"));
        personData.add(new Person("Martin", "Mueller"));
	}

	/**
	 * Returns the data as an observable list of Persons.
	 * @return
	 */
	public ObservableList<Person> getPersonData() {
		return personData;
	}

	@Override
	public void start(Stage primaryStage) {
		this.primaryStage = primaryStage;
		this.primaryStage.setTitle("AddressApp");

		initRootLayout();
		showPersonOverview();
	}

	/**
	 * Initializes the root layout.
	 */
	public void initRootLayout(){
		// Load root layout from fxml file.
		FXMLLoader loader = new FXMLLoader();
		loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
		try {
			rootLayout = (BorderPane) loader.load();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// Show the scene containing the root layout.
		Scene scene = new Scene(rootLayout);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	/**
	 * Shows the person overview inside the root layout.
	 */
	public void showPersonOverview(){
		// Load person overview.
		FXMLLoader loader = new FXMLLoader();
		loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml"));
		try {
			AnchorPane personOverview = (AnchorPane) loader.load();
			rootLayout.setCenter(personOverview);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// Give the controller access to the main app.
		PersonOverviewController controller = loader.getController();
		controller.setMainApp(this);
	}

	/**
	 * Opens a dialog to edit details for the specified person. if the user
	 * clicks OK, the changes are saved into the provided person object and true
	 * is returned.
	 *
	 * @param person : the person object to be edited
	 * @return : true if the user clicked OK, false otherwise.
	 */
	public boolean showPersonEditDialog(Person person){
		// Load the fxml file and create a new stage for the popup dialog.
		FXMLLoader loader = new FXMLLoader();
		loader.setLocation(MainApp.class.getResource("view/PersonEditDialog.fxml"));
		AnchorPane page = null;
		try {
			page = (AnchorPane) loader.load();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		// Create the dialog Stage.
		Stage dialogStage = new Stage();
		dialogStage.setTitle("Edit Person");
		dialogStage.initModality(Modality.WINDOW_MODAL);
		dialogStage.initOwner(primaryStage);
		Scene scene = new Scene(page);
		dialogStage.setScene(scene);

		// Set the person into the controller.
		PersonEditDialogController controller = loader.getController();
		controller.setDialogStage(dialogStage);
		controller.setPerson(person);

		// Show the dialog and wait until the user closes it
		dialogStage.showAndWait();

		return controller.isOkClicked();
	}

	/**
	 * Returns the main stage.
	 * @return
	 */
	public Stage getPrimaryStage() {
		return primaryStage;
	}

	public static void main(String[] args) {
		launch(args);
	}
}
